package org.example.env_boot.controller;

import org.example.env_boot.dto.ApiResponse;
import org.example.env_boot.entity.AqiFeedback;
import org.example.env_boot.entity.Statistics;
import org.example.env_boot.service.GridMemberService;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.NotNull;
import java.util.List;

/**
 * 网格员控制器
 */
@RestController
@RequestMapping("/grid")
@CrossOrigin(origins = "*")
public class GridMemberController {
    
    private final GridMemberService gridMemberService;
    
    public GridMemberController(GridMemberService gridMemberService) {
        this.gridMemberService = gridMemberService;
    }
    
    /**
     * 获取分配给当前网格员的反馈信息
     */
    @GetMapping("/feedbacks/assigned")
    public ApiResponse<List<AqiFeedback>> getAssignedFeedbacks(Authentication authentication) {
        Integer gmId = Integer.parseInt(authentication.getDetails().toString()); // 从JWT中获取网格员ID
        return gridMemberService.getAssignedFeedbacks(gmId);
    }
    
    /**
     * 获取网格员处理过的所有反馈信息
     */
    @GetMapping("/feedbacks/processed")
    public ApiResponse<List<AqiFeedback>> getProcessedFeedbacks(Authentication authentication) {
        Integer gmId = Integer.parseInt(authentication.getDetails().toString());
        return gridMemberService.getProcessedFeedbacks(gmId);
    }
    
    /**
     * 确认AQI数据
     */
    @PostMapping("/feedbacks/{feedbackId}/confirm")
    public ApiResponse<Statistics> confirmAQIData(@PathVariable Integer feedbackId,
                                                  @RequestBody ConfirmAQIRequest request,
                                                  Authentication authentication) {
        Integer gmId = Integer.parseInt(authentication.getDetails().toString());
        
        return gridMemberService.confirmAQIData(
            gmId,
            feedbackId,
            request.getSo2Value(),
            request.getCoValue(),
            request.getSpmValue(),
            request.getAqiId()
        );
    }
    
    /**
     * 获取工作统计信息
     */
    @GetMapping("/statistics")
    public ApiResponse<GridMemberService.WorkStatistics> getWorkStatistics(Authentication authentication) {
        Integer gmId = Integer.parseInt(authentication.getDetails().toString());
        return gridMemberService.getWorkStatistics(gmId);
    }
    
    /**
     * 获取反馈详情
     */
    @GetMapping("/feedbacks/{feedbackId}")
    public ApiResponse<AqiFeedback> getFeedbackDetail(@PathVariable Integer feedbackId,
                                                      Authentication authentication) {
        Integer gmId = Integer.parseInt(authentication.getDetails().toString());
        return gridMemberService.getFeedbackDetail(gmId, feedbackId);
    }
    
    // 请求类定义
    public static class ConfirmAQIRequest {
        @NotNull(message = "SO2数值不能为空")
        private Integer so2Value;
        
        @NotNull(message = "SO2级别不能为空")
        private Integer so2Level;
        
        @NotNull(message = "CO数值不能为空")
        private Integer coValue;
        
        @NotNull(message = "CO级别不能为空")
        private Integer coLevel;
        
        @NotNull(message = "PM2.5数值不能为空")
        private Integer spmValue;
        
        @NotNull(message = "PM2.5级别不能为空")
        private Integer spmLevel;
        
        @NotNull(message = "AQI级别ID不能为空")
        private Integer aqiId;
        
        public Integer getSo2Value() { return so2Value; }
        public void setSo2Value(Integer so2Value) { this.so2Value = so2Value; }
        public Integer getSo2Level() { return so2Level; }
        public void setSo2Level(Integer so2Level) { this.so2Level = so2Level; }
        public Integer getCoValue() { return coValue; }
        public void setCoValue(Integer coValue) { this.coValue = coValue; }
        public Integer getCoLevel() { return coLevel; }
        public void setCoLevel(Integer coLevel) { this.coLevel = coLevel; }
        public Integer getSpmValue() { return spmValue; }
        public void setSpmValue(Integer spmValue) { this.spmValue = spmValue; }
        public Integer getSpmLevel() { return spmLevel; }
        public void setSpmLevel(Integer spmLevel) { this.spmLevel = spmLevel; }
        public Integer getAqiId() { return aqiId; }
        public void setAqiId(Integer aqiId) { this.aqiId = aqiId; }
    }
} 